<?php

namespace App\Http\Resources\Train\Station;

use App\Models\Train\Stations\Repositories\Interfaces\StationRepositoryInterface;
use App\Models\Train\Stations\Repositories\StationRepository;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class PassPointsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array|Arrayable|JsonSerializable
     * @throws Exception
     */
    public function toArray($request)
    {
        /**
         * @var StationRepository $stationRepo
         */
        $stationRepo            = app()->make(StationRepositoryInterface::class);
        $ret                    = parent::toArray($request);
        $points_count           = $request->offsetGet('points_count');
        $from_station_code      = $request->input('from_station_code');
        $to_station_code        = $request->input('to_station_code');
        $station                = $stationRepo->findOneByCode($ret['station_code']);
        $ret['station_name_uy'] = $station?->getStationNameUy();
        $from_station_code && ($ret['is_from'] = $ret['station_code'] == $from_station_code ? 1 : 0);
        $to_station_code && ($ret['is_to'] = $ret['station_code'] == $to_station_code ? 1 : 0);
        $ret['is_start'] = $ret['sequence'] == 1 ? 1 : 0;
        $ret['is_end']   = $points_count == $ret['sequence'] ? 1 : 0;
        return $ret;
    }
}
